<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/10
 * Time: 10:57
 */
namespace app\admin\controller;


use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use app\admin\model\AdminUser;
use think\facade\Session;

class IndexController extends BaseController
{

    //渲染Index页面
    public function index(){
        $admin_id = Session::get('admin_id');
    	$admin = AdminUser::where('id',$admin_id)->field('id,username,role')->find();
    	$role  = AdminRole::where('id',$admin['role'])->where('status',1)->find();
        $role && $role['rule'] && $role['rule'] = json_decode($role['rule']);
        $rule = AdminMenu::where('id','in',$role['rule'])->where('type',1)->select()->toArray();
    	$ret = $this->roleArray($rule);
    	foreach ($ret as $key=>$value){
    	    if ($value['controller'] && $value['method']){
                $ret[$key]['url'] = $value['controller'] ."/". $value['method'];
            }else{
                $ret[$key]['url'] = '';
            }
    	    if (is_array($value['children'])){
                foreach ($value['children'] as $k=>$v){
                    $ret[$key]['children'][$k]['url'] = $v['controller']."/".$v['method'];
                }
            }else{
                $ret[$key]['children'] = [];
            }
        }
//    	dump($ret);die();
    	$this->assign('admin',$admin);
    	$this->assign('menu',$ret);
        return $this->fetch();
    }

    public function welcome(){
        $admin_id = Session::get('admin_id');
        $admin = AdminUser::where('id',$admin_id)->field('id,username')->find();
        $this->assign('admin',$admin);
        return $this->fetch();
    }


}